//
//  SliderViewController.swift
//  Exchange
//
//  Created by rui on 2018/5/23.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class SliderViewController: UIViewController {

    var reuseIdentifier = "SliderCellTableViewCell"
    
    var dataArray = [TradeCoinModel]()
    
    typealias Completion = (String)->()
    var completion: Completion?
    
    lazy var contentView: UIView = {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: 90, height: self.view.height))
        view.autoresizingMask = .flexibleHeight
        view.themeBackground = ThemeManager.ThemeColorKeys.background
        return view
    }()
    
    lazy var titleView: UIView = {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: self.contentView.width, height: kNavi_HEIGHT))
        let imageView = UIImageView()
        imageView.themeImage = ThemeManager.ThemeImageKeys.coinRetractRight
        view.themeBackground = ThemeManager.ThemeColorKeys.background
        imageView.frame = CGRect(x: self.contentView.width - 44, y: kNavi_HEIGHT - 44, width: 44, height: 44)
        imageView.contentMode = UIViewContentMode.center
        view.addSubview(imageView)
        let line = UIView(frame: CGRect(x: 0, y: kNavi_HEIGHT - CGFloat(0.5), width: self.contentView.width, height: 0.5))
        line.themeBackground = ThemeManager.ThemeColorKeys.separator
        view.addSubview(line)
        view.isUserInteractionEnabled = true
        imageView.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer(target: self, action: #selector(godismiss))
        view.addGestureRecognizer(tap)
        return view
    }()
    
    lazy var tableView: UITableView = {
        let table = UITableView(frame: CGRect(x: 0, y: kNavi_HEIGHT, width: self.contentView.width, height: self.view.height - kNavi_HEIGHT), style: .plain)
        table.autoresizingMask = .flexibleHeight
        table.delegate = self
        table.dataSource = self
        table.themeBackground = ThemeManager.ThemeColorKeys.background
        table.themeSeparatorColor = ThemeManager.ThemeColorKeys.separator
        table.register(UINib(nibName: reuseIdentifier, bundle: nil), forCellReuseIdentifier: reuseIdentifier)
        table.tableFooterView = UIView()
        return table
    }()

    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.addSubview(contentView)
        contentView.addSubview(titleView)
        contentView.addSubview(tableView)
        
        AppContext.default.getTradeCoins { (models) in
            self.dataArray = models
            self.tableView.reloadData()
        }
        self.view.backgroundColor = UIColor(white: 0, alpha: 0.2)
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.dismiss(animated: true, completion: nil)
    }
    
    @objc func godismiss() {
        self.dismiss(animated: true, completion: nil)
    }
}

extension SliderViewController: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as! SliderCellTableViewCell
        cell.configerModel(model: dataArray[indexPath.row])
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: false)
        let model = dataArray[indexPath.row]
        self.completion?(model.coinCode ?? "")
        godismiss()
    }
}
